{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "arr = np.ones([10, 10])\n",
    "zero = np.zeros(10, 10)\n",
    "\n",
    "print(arr)\n",
    "\n",
    "# 大小, 形状, 维数, 数据类型\n",
    "print(arr.size, arr.shape, arr.ndim, arr.dtype)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "\n",
    "print(arr.size, arr.shape, arr.ndim, arr.dtype)\n",
    "\n",
    "print(arr)\n",
    "\n",
    "arr_slice = arr[2:5]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "arr1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "arr2 = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
    "\n",
    "# arr_slice = arr1[1:5]\n",
    "arr_slice = arr2[1:5]  # 切片是浅复制\n",
    "\n",
    "arr_slice[0] = 10\n",
    "# print(arr_slice)\n",
    "print(arr2)\n",
    "\n",
    "print(arr1[0])\n",
    "\n",
    "# 行相加\n",
    "print(arr1.sum(axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 生成矩阵\n",
    "# print(np.arange(6).reshape(3, 1, 2))\n",
    "\n",
    "a = np.arange(4)\n",
    "b = np.array([10, 20, 30, 40])\n",
    "\n",
    "# 矩阵数乘\n",
    "print(a)\n",
    "print(b)\n",
    "print(b - a)\n",
    "print(a**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([[1, 2], [3, 4]])\n",
    "b = np.array([[1, 0], [0, 1]])\n",
    "\n",
    "#  矩阵的乘法 @  .dot()\n",
    "c = a @ b\n",
    "d = a.dot(b)\n",
    "print(c)\n",
    "print(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "a = np.ones((2, 3), dtype=int)\n",
    "b = np.arange(6).reshape(2, 3)\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "\n",
    "a += 3\n",
    "print(a)\n",
    "\n",
    "b += a\n",
    "print(b)\n",
    "\n",
    "a.dtype.name\n",
    "b.dtype.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "a = np.arange(9, dtype=float).reshape(3, 3)\n",
    "\n",
    "print(a.sum())\n",
    "print(a.min())\n",
    "print(a.max())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "a = np.arange(27).reshape(3, 3, 3)\n",
    "\n",
    "# numpy 切片\n",
    "print(a)\n",
    "# print(a[1, ...])\n",
    "# print(a[..., 1])\n",
    "\n",
    "print(a[:, :, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "a = np.arange(9).reshape(3, 3)\n",
    "\n",
    "\n",
    "# for row in a:\n",
    "#     print(row)\n",
    "\n",
    "for element in a.flat:\n",
    "    print(element)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "arr = np.arange(9)\n",
    "\n",
    "# 不复制, 引用\n",
    "b = arr \n",
    "\n",
    "# 切片 浅复制\n",
    "c = arr[5:8]\n",
    "\n",
    "# 深复制\n",
    "d = arr.copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.  2.  3.]\n",
      " [11. 12. 13.]\n",
      " [21. 22. 23.]\n",
      " [31. 32. 33.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "\n",
    "a = np.arange(4).reshape(4, 1)\n",
    "b = np.ones([4, 4], dtype=np.int64)\n",
    "\n",
    "# print(a)\n",
    "# print(b)\n",
    "\n",
    "# 广播\n",
    "# print((a.T + b).T)\n",
    "\n",
    "# print(a * b)\n",
    "\n",
    "\n",
    "a = np.array([0.0, 10.0, 20.0, 30.0]).reshape(4, 1)\n",
    "b = np.array([1.0, 2.0, 3.0])\n",
    "\n",
    "print(a + b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[[0 1]\n",
      " [2 3]]\n",
      "[[1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]]\n",
      "[[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n",
      "[[3 3]\n",
      " [3 3]]\n",
      "[[1. 0. 0. 0.]\n",
      " [0. 1. 0. 0.]\n",
      " [0. 0. 1. 0.]\n",
      " [0. 0. 0. 1.]]\n",
      "[1. 5. 9.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "a = np.arange(10)\n",
    "b= np.arange(4).reshape(2, 2)\n",
    "c = np.ones([4, 4])\n",
    "d = np.zeros([4, 4])\n",
    "e = np.full([2, 2], 3)\n",
    "f = np.eye(4, 4)\n",
    "g = np.linspace(1, 9, 3)\n",
    "\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)\n",
    "print(d)\n",
    "print(e)\n",
    "print(f)\n",
    "print(g)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5, 10)\n",
      "(10, 5)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "\n",
    "x, y = np.mgrid[0:10, 0:5]\n",
    "print(x.shape[::-1])\n",
    "print(y.shape)\n",
    "\n",
    "x1 = np.mgrid[0:1:.1]\n",
    "# print(x1)\n",
    "x2 = np.mgrid[0:10:5j]\n",
    "# print(x2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[0 0 0 0 0 0 0]\n",
      "  [1 1 1 1 1 1 1]\n",
      "  [2 2 2 2 2 2 2]\n",
      "  [3 3 3 3 3 3 3]\n",
      "  [4 4 4 4 4 4 4]]\n",
      "\n",
      " [[0 1 2 3 4 5 6]\n",
      "  [0 1 2 3 4 5 6]\n",
      "  [0 1 2 3 4 5 6]\n",
      "  [0 1 2 3 4 5 6]\n",
      "  [0 1 2 3 4 5 6]]]\n",
      "[[[0 0]\n",
      "  [1 0]\n",
      "  [2 0]\n",
      "  [3 0]\n",
      "  [4 0]]\n",
      "\n",
      " [[0 1]\n",
      "  [1 1]\n",
      "  [2 1]\n",
      "  [3 1]\n",
      "  [4 1]]\n",
      "\n",
      " [[0 2]\n",
      "  [1 2]\n",
      "  [2 2]\n",
      "  [3 2]\n",
      "  [4 2]]\n",
      "\n",
      " [[0 3]\n",
      "  [1 3]\n",
      "  [2 3]\n",
      "  [3 3]\n",
      "  [4 3]]\n",
      "\n",
      " [[0 4]\n",
      "  [1 4]\n",
      "  [2 4]\n",
      "  [3 4]\n",
      "  [4 4]]\n",
      "\n",
      " [[0 5]\n",
      "  [1 5]\n",
      "  [2 5]\n",
      "  [3 5]\n",
      "  [4 5]]\n",
      "\n",
      " [[0 6]\n",
      "  [1 6]\n",
      "  [2 6]\n",
      "  [3 6]\n",
      "  [4 6]]]\n",
      "[[0 0]\n",
      " [1 0]\n",
      " [2 0]\n",
      " [3 0]\n",
      " [4 0]\n",
      " [0 1]\n",
      " [1 1]\n",
      " [2 1]\n",
      " [3 1]\n",
      " [4 1]\n",
      " [0 2]\n",
      " [1 2]\n",
      " [2 2]\n",
      " [3 2]\n",
      " [4 2]\n",
      " [0 3]\n",
      " [1 3]\n",
      " [2 3]\n",
      " [3 3]\n",
      " [4 3]\n",
      " [0 4]\n",
      " [1 4]\n",
      " [2 4]\n",
      " [3 4]\n",
      " [4 4]\n",
      " [0 5]\n",
      " [1 5]\n",
      " [2 5]\n",
      " [3 5]\n",
      " [4 5]\n",
      " [0 6]\n",
      " [1 6]\n",
      " [2 6]\n",
      " [3 6]\n",
      " [4 6]]\n",
      "[[0. 0. 0.]\n",
      " [1. 0. 0.]\n",
      " [2. 0. 0.]\n",
      " [3. 0. 0.]\n",
      " [4. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [1. 1. 0.]\n",
      " [2. 1. 0.]\n",
      " [3. 1. 0.]\n",
      " [4. 1. 0.]\n",
      " [0. 2. 0.]\n",
      " [1. 2. 0.]\n",
      " [2. 2. 0.]\n",
      " [3. 2. 0.]\n",
      " [4. 2. 0.]\n",
      " [0. 3. 0.]\n",
      " [1. 3. 0.]\n",
      " [2. 3. 0.]\n",
      " [3. 3. 0.]\n",
      " [4. 3. 0.]\n",
      " [0. 4. 0.]\n",
      " [1. 4. 0.]\n",
      " [2. 4. 0.]\n",
      " [3. 4. 0.]\n",
      " [4. 4. 0.]\n",
      " [0. 5. 0.]\n",
      " [1. 5. 0.]\n",
      " [2. 5. 0.]\n",
      " [3. 5. 0.]\n",
      " [4. 5. 0.]\n",
      " [0. 6. 0.]\n",
      " [1. 6. 0.]\n",
      " [2. 6. 0.]\n",
      " [3. 6. 0.]\n",
      " [4. 6. 0.]]\n"
     ]
    }
   ],
   "source": [
    "import  numpy as np\n",
    "\n",
    "w = 5\n",
    "h = 7\n",
    "\n",
    "objp = np.zeros((w * h, 3), np.float32)\n",
    "objp[:, :2] = np.mgrid[0:w, 0:h].T.reshape(-1, 2)\n",
    "# print(objp)\n",
    "# print(objp.shape)\n",
    "print(np.mgrid[0:w, 0:h])\n",
    "print(np.mgrid[0:w, 0:h].T)\n",
    "print(np.mgrid[0:w, 0:h].T.reshape(-1, 2))\n",
    "print(objp)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.12 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "6a018d3a992d78c184ccc94ab54168c7b26325ed3c2283926339c3edbf5487e7"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}